package ntd.action.bo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import ntd.tool.ToolClass;

/**
 * action 入参
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class ActionInBo {

    /**
     * 数据编码
     */
    private String dataCode;

    /**
     * 数据字符串
     */
    private String dataStr = "";

    /**
     * 参数
     */
    private Dict param = Dict.create();

    /**
     * 数据
     */
    private Dict data = Dict.create();

    /**
     * 多类型数据
     */
    private Object dataObj;

    public <T> T getDataObj() {
        return (T) dataObj;
    }

    public ActionInBo(String dataCode) {
        this.dataCode = dataCode;
    }

    public ActionInBo(String dataCode, Dict param) {
        this.dataCode = dataCode;
        this.param = param;
    }

    public <T> T t(Class<T> clazz) {
        T o = ToolClass.t(clazz);
        BeanUtil.fillBeanWithMap(data, o, true);
        BeanUtil.fillBeanWithMap(param, o, true);
        if (JSONUtil.isTypeJSONObject(this.dataStr)) {
            BeanUtil.fillBeanWithMap(JSONUtil.parseObj(this.dataStr), o, true);
        }
        return o;
    }

    public String dsId() {
        return this.getParamStr("_dsId");
    }

    public String getParamStr(String key) {
        return ObjectUtil.defaultIfBlank(this.data.getStr(key), this.param.getStr(key));
    }

}
